const DB = require("../model/db.js");

const { 
	GraphQLObjectType,
	GraphQLString,
	GraphQLInt,
	GraphQLList,
	GraphQLSchema
} = require("graphql");

// 定义nav导航的schema
var NavSchema = new GraphQLObjectType({
	name: "nav",
	fields: {
		_id: {
			type: GraphQLString
		},
		title: {
			type: GraphQLString
		},
		url: {
			type: GraphQLString
		},
		sort: {
			type: GraphQLInt
		},
		status: {
			type: GraphQLString
		},
		add_time: {
			type: GraphQLString
		}
	}
})

// 定义一个根 配置调用Schema的方法

var RootSchema = new GraphQLObjectType({
	name: "root",
	fields: {
		navList: {
			type: GraphQLList(NavSchema),
			async resolve(parent, args) {
				var navList = await DB.find("nav", {});
				return navList;
			}
		},
		oneNavList: {
			type: NavSchema,
			args: {
				_id: { type: GraphQLString },
				status: { type: GraphQLString }
			},
			
			async resolve(parent, args) {
				var oneNavList = await DB.find('nav', {"_id": DB.getObjectId(args._id),"status":args.status});
				
				return oneNavList[0];
			}
		},
		
	}
})


module.exports = new GraphQLSchema({
	query: RootSchema
})


